/**
 * Created by an.han on 13-12-17.
 */
window.onload = function () {
	var productArray = {};
	productStr=$.cookie('productArray');
	productArray = productStr ? str2Array(productStr) : null;
	$.each(productArray,function(id,detail) {
	    //添加到表单
	    var productHtml = "";
	    productHtml += "<tr id='"+detail[0]+"'>";
	    //productHtml += "<td class='checkbox'><input class='check-one check' type='checkbox'/></td>";
	    productHtml += "<td class='goods'><a href='"+ detail[3] +"' target='_top'><img src='"+detail[2]+"' alt=''/></a><span>"+detail[1]+"</span></td>";
	    productHtml += "<td class='operation'><span class='delete'>Remove</span></td>";
	    productHtml += "</tr>";
	    $(".product_list").append(productHtml);
	}); 
    if (!document.getElementsByClassName) {
        document.getElementsByClassName = function (cls) {
            var ret = [];
            var els = document.getElementsByTagName('*');
            for (var i = 0, len = els.length; i < len; i++) {

                if (els[i].className.indexOf(cls + ' ') >=0 || els[i].className.indexOf(' ' + cls + ' ') >=0 || els[i].className.indexOf(' ' + cls) >=0) {
                    ret.push(els[i]);
                }
            }
            return ret;
        }
    }

    var table = document.getElementById('cartTable'); // 购物车表格
    var selectInputs = document.getElementsByClassName('check'); // 所有勾选框
    var checkAllInputs = document.getElementsByClassName('check-all') // 全选框
    var tr = table.children[1].rows; //行
    var selectedTotal = document.getElementById('selectedTotal'); //已选商品数目容器
    var deleteAll = document.getElementById('deleteAll'); // 删除全部按钮
    var selectedViewList = document.getElementById('selectedViewList'); //浮层已选商品列表容器
    var selected = document.getElementById('selected'); //已选商品
    var foot = document.getElementById('foot');
    var closing = document.getElementById('closing');

    // 更新总数，已选浮层
    function getTotal() {
        var selected = 0, price = 0, html = '';
        for (var i = 0; i < tr.length; i++) {
            if (tr[i].getElementsByTagName('input')[0].checked) {
                tr[i].className = 'on';
                selected += 1; //计算已选商品数目
                html += '<div><img src="'+tr[i].getElementsByTagName('img')[0].src+'"><span class="del" index="'+i+'">取消选择</span></div>';// 添加图片到弹出层已选商品列表容器
            }else{
                tr[i].className = '';
            }
        }

        selectedTotal.innerHTML = selected; // 已选数目
        selectedViewList.innerHTML = html;
        if (selected==0) {
            foot.className = 'foot';
        }
    }
    // 点击选择框
    for(var i = 0; i < selectInputs.length; i++ ){
        selectInputs[i].onclick = function () {
            if (this.className.indexOf('check-all') >= 0) { //如果是全选，则吧所有的选择框选中
                for (var j = 0; j < selectInputs.length; j++) {
                    selectInputs[j].checked = this.checked;
                }
            }
            if (!this.checked) { //只要有一个未勾选，则取消全选框的选中状态
                for (var i = 0; i < checkAllInputs.length; i++) {
                    checkAllInputs[i].checked = false;
                }
            }
            getTotal();//选完更新总计
        }
    }

    // 显示已选商品弹层
    selected.onclick = function () {
        if (selectedTotal.innerHTML != 0) {
            foot.className = (foot.className == 'foot' ? 'foot show' : 'foot');
        }
    }

    //已选商品弹层中的取消选择按钮
    selectedViewList.onclick = function (e) {
        var e = e || window.event;
        var el = e.srcElement;
        if (el.className=='del') {
            var input =  tr[el.getAttribute('index')].getElementsByTagName('input')[0]
            input.checked = false;
            input.onclick();
        }
    }

  //为每行元素添加事件
    for (var i = 0; i < tr.length; i++) {
        //将点击事件绑定到tr元素
        tr[i].onclick = function (e) {
            var e = e || window.event;
            var el = e.target || e.srcElement; //通过事件对象的target属性获取触发元素
            var tid = this.id;
            var cls = el.className; //触发元素的class
            //通过判断触发元素的class确定用户点击了哪个元素
            switch (cls) {
                case 'delete': //点击了删除
                    var conf = confirm('Remove this product?');
                    if (conf) {
                        //删除cookie信息
                    	var newProductArray = {};
                    	$.each(productArray,function(name,value) {
                    		if(name == tid)
                    			return true;
                    		newProductArray[name] = value;
                    	}); 
                    	if(newProductArray.length > 0){
                        	//保存到cookie
                        	$.cookie('productArray', obj2Str(newProductArray), {"path":"/"});
                    	}else{

                        	$.cookie('productArray', null, {"path":"/"});
                    	}
                        this.parentNode.removeChild(this);
                    }
                    break;
            }
            getTotal();
        }
    }
    

    // 点击全部删除
    deleteAll.onclick = function () {
        if (selectedTotal.innerHTML != 0) {
            var con = confirm('Delect this products?'); //弹出确认框
            if (con) {
            	var tidArray = [];
                for (var i = 0; i < tr.length; i++) {
                    // 如果被选中，就删除相应的行
                    if (tr[i].getElementsByTagName('input')[0].checked) {
                    	tidArray.push(tr[i].id);
                        tr[i].parentNode.removeChild(tr[i]); // 删除相应节点
                        i--; //回退下标位置
                    }
                }

                //删除cookie信息
            	var newProductArray = {};
            	/*alert(tidArray);
            	alert(tidArray.indexOf("1"));*/
            	$.each(productArray,function(name,value) {
            		if(tidArray.indexOf(name) != -1){
            			return true;
            		}
            		newProductArray[name] = value;
            	});
            	if(newProductArray.length>0){
                	//保存到cookie
                	$.cookie('productArray', obj2Str(newProductArray), {"path":"/"});
            	}else{

                	$.cookie('productArray', null);
            	}
            }
        } else {
            alert('Please select product!');
        }
        getTotal(); //更新总数
    }
    closing.onclick = function(){
    	window.location.href = "/index.php?m=content&c=index&a=send_enquiry";
    }

    // 默认全选
    checkAllInputs[0].checked = true;
    checkAllInputs[0].onclick();
}
